<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ include file="/WEB-INF/views/commons/taglibs.jsp"%>
<!doctype html>
<html>
	<head>
		<title>列表</title>
	</head>
	<body>
		<div class="row">
			<form role="form" class="col-md-8" action="${ctx}/generate/tableList" method="post">
				<div class="form-group form-inline">
					<label for="name">
						数据库名
					</label>
					<input type="text" class="form-control" value="${dbName}"
						name="dbName" placeholder="请输入数据库名">
					<input type="submit" class="form-control" value="查询">
				</div>
			</form>
			<div class="col-md-4">
				<button class="btn btn-primary pull-right" data-toggle="modal" data-target="#slctTemplateModal"> 代码生成 </button>
			</div>
		</div>
		<table class="table table-striped table-bordered table-hover">
			<caption>
				数据库表
			</caption>
			<thead>
				<tr>
					<td></td> <td> tableName </td> <td> tableNameNoDash </td> <td> tableNameCapitalized </td> <td> tableComment </td> <td> 操作 </td>
				</tr>
			</thead>
			<c:forEach items="${resultMap.data}" var="bean">
				<tr>
					<td> <input type="checkbox" value="${bean.tableName}" name="slctTbs"> </td> <td> ${bean.tableName } </td>
					<td> ${bean.tableNameNoDash} </td> <td> ${bean.tableNameCapitalized} </td>
					<td> ${bean.tableComment } </td>
					<td> <button class="btn-link" onclick="tableInfo('${bean.tableName}')"> 详细 </button> </td>
				</tr>
			</c:forEach>
		</table>
		
		<!-- 模态框（Modal） -->
		<div class="modal fade" id="slctTemplateModal" tabindex="-1" role="dialog"
			aria-labelledby="myModalLabel" aria-hidden="true">
			<div class="modal-dialog">
				<form action="${ctx}/generate/" method="post" id="generateForm" name="generateForm">
				<div class="modal-content">
					<div class="modal-header">
						<button type="button" class="close" data-dismiss="modal" aria-hidden="true"> &times; </button>
						<h4 class="modal-title" id="myModalLabel">选择模板 </h4>
					</div>
					<div class="modal-body" style="height: 60%; overflow: auto;">
						<div class="col-md-6">
							<label>选择模板:</label>
							<select class="form-control form-group" name="tempPath" id="tempSlct" datatype="*">
								<option value="">请选择模板</option>
							</select>
							<input name="dbName" value="${dbName}" type="hidden">
							<label>输出路径绝对地址:</label>
							<input class="form-control form-group" name="outputPath" value="${outputPath}" placeholder="输出路径绝对地址" 
							datatype="*" errormsg="输出路径不能为空"/>
							<label>package:</label>
							<input class="form-control form-group" name="packageBase" value="${packageBase}"  placeholder="package" datatype="s"/>
						</div>
						<div class="col-md-6">
							<ul class="list-group" id="template"></ul>
							<textarea class="form-control" rows="5" id="templDiscription"></textarea>
						</div>
					</div>
					<div class="modal-footer">
						<button type="button" class="btn btn-default" data-dismiss="modal"> 关闭 </button>
						<button type="button" class="btn btn-primary" id="btn_sub">生成</button>
					</div>
				</div>
				</form>
				<!-- /.modal-content -->
			</div>
			<!-- /.modal -->
		</div>
		<script type="text/javascript">
		
		$(function(){
			$("#generateForm").Validform({
				btnSubmit:"#btn_sub",
				tiptype:4,
				ajaxPost:true,
				beforeSubmit:function(form){
				var obj = document.getElementById("generateForm");
					var slctTb=[];
					$(":checked[name='slctTbs']").each(function(){
						slctTb.push($(this).val());
						});
						var param={
						slctTb:slctTb,
						tempPath:obj.tempPath.value,
						outputPath:obj.outputPath.value,
						packageBase:obj.packageBase.value,
						dbName:obj.dbName.value
						};
					var layerLoadIndex= layer.load(1);
					$.post(obj.action,param,function(d){
						if(d.success){
							layer.close(layerLoadIndex);
							layer.alert(d.msg);
							}
						},"json");
					return false;
				}
			});
			ajaxTemplate(false,function(d){
				if(d.success){
					var html=[],temp;
					for(var i in d.data){
						temp = d.data[i].substr(d.data[i].lastIndexOf('\\')+1)
						html.push('<option value="'+temp+'">'+temp+'</option>');
						}
					$("#tempSlct").append(html.join(""))
					.change(function(){
						if($(this).val()){
							ajaxTemplate($(this).val(),function(d){
								if(d.success){
									var html=[],temp;
									for(var i in d.data){
										temp = d.data[i].substr(d.data[i].lastIndexOf('\\')+1)
										html.push('<li class="list-group-item">'+temp+'</li>');
										}
									$("#template").empty().append(html.join(""))
									$("#templDiscription").text(d.msg).removeClass("hidden");
									}
								});
							}
						});
					
					}
				});
			function ajaxTemplate(tempPath,callback){
				var param=null;
				if(tempPath){ param={'tempPath':tempPath}; }
				$.post("${ctx}/generate/ajax/template",param,function(d){
					callback(d);
					},"json");
				}
			});
		var iframe, src = "${ctx}/generate/ajax/tableForm?dbName=${dbName}&tbName=";
		function tableInfo(tableName) {
			if (!iframe) {
				iframe = layer.open( {
					type : 2,
					title : tableName,
					shadeClose : true,
					shade : false,
					maxmin : true, //开启最大化最小化按钮
					area : [ '800px', '600px' ],
					offset : '10%',
					move : false,
					content : src + tableName,
					end : function() {
						iframe = null;
					}
				});
			} else {
				layer.iframeSrc(iframe, src + tableName)
			}
		}
	</script>
	</body>
</html>
